<template>
  <div class="newslist-contain">
    <van-loading size="24px" vertical v-if="isLoading">加载中...</van-loading>
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
      <ul>
        <li v-for="item in newslist" :key="item.id" @click="$router.push(`/newsparticulars/${item.id}`)">
          <img :src="item.img_url" />
          <div class="text">
            <div class="title">{{ item.title }}</div>
            <div class="explain">
              <span>发布时间：{{ $moment(item.add_time).format("YYYY-MM-DD") }}</span>
              <span>点击次数：{{ item.click }}</span>
            </div>
          </div>
        </li>
      </ul>
    </van-pull-refresh>
    <van-divider v-if="isLoadDone">已经全部加载</van-divider>
    <!-- <div>
      <van-button type="info" @click="load">加载</van-button>
    </div> -->
  </div>
</template>

<script>
import { getNewsList } from "@/api";

export default {
  data() {
    return {
      page: 1,
      pagesize: 10,
      newslist: [],
      isLoading: false,
      isLoadDone: false,
    };
  },
  methods: {
    // async load() {
    //   this.page++;
    //   let { message } = await getNewsList(this.page, this.pagesize);
    //   if (message.length === 0) {
    //     this.$toast("亲，已经没有数据了");
    //     return;
    //   }
    //   this.newslist = [...this.newslist, ...message];
    // },
    async loadGoodsList() {
      if (this.isLoadDone) {
        return;
      }
      //滚上去的高度
      let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
      //   网页视口的宽度
      let windowHeight = document.documentElement.clientHeight || document.body.clientHeight;
      //整个页面的高度
      let pageHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
      if (Math.ceil(scrollTop) + windowHeight === pageHeight) {
        this.page++;
        let { message } = await getNewsList(this.page, this.pagesize);
        if (message.length === 0) {
          this.isLoadDone = true;
          return;
        }
        this.newslist = [...this.newslist, ...message];
      }
    },
    async onRefresh() {
      this.isLoading = true;
      this.page = 1;
      this.newslist = [];
      let { message } = await getNewsList(this.page, this.pagesize);
      this.isLoading = false;
      this.newslist = message;
    },
    async getNewsList() {
      let { message } = await getNewsList(this.page, this.pagesize);
      this.newslist = message;
    },
  },
  mounted() {
    window.addEventListener("scroll", this.loadGoodsList);
    this.getNewsList();
  },
  // eslint-disable-next-line vue/no-deprecated-destroyed-lifecycle
  beforeDestroy() {
    window.removeEventListener("scroll", this.loadGoodsList);
  },
};
</script>

<style lang="scss" scoped>
.newslist-contain {
  padding: 10px;

  .van-button {
    width: 100%;
  }

  ul {
    li {
      display: flex;
      justify-content: space-between;
      width: 100%;
      height: 104px;
      padding-top: 3px;
      border-bottom: 1px solid #ccc;

      img {
        width: 100px;
        height: 100%;
      }

      .text {
        display: flex;
        flex: 1;
        flex-direction: column;
        justify-content: space-between;
        padding: 10px 5px;

        .title {
          font-size: 14px;
          font-weight: 700;
        }

        .explain {
          display: flex;
          justify-content: space-between;
          font-size: 12px;
        }
      }
    }
  }
}
</style>
